﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RichLabelSpace
{
    namespace Warp
    {
        internal enum RichStringType
        {
            SimpleLab,
            Lab,
            Spr,
            SprAmin,
            Tex,
            Url,
            Br,
        }

        internal struct RichStringSign
        {
            public readonly string StartSign;
            public readonly string EndSign;
            public RichStringSign(string start_sign, string end_sign)
            {
                this.StartSign = start_sign;
                this.EndSign = end_sign;
            }
        }
        internal static class RichStringTools
        {
            private static bool is_init = false;
            internal static Dictionary<RichStringType, RichStringSign> match_type_dict = new Dictionary<RichStringType, RichStringSign>
            {
            {RichStringType.Lab, new RichStringSign("<label","</label>")},
            {RichStringType.Url,  new RichStringSign("<url","</url>")},
            {RichStringType.Spr,  new RichStringSign("<sprite","/>")},
            {RichStringType.SprAmin,  new RichStringSign("<spamin","/>")},
            {RichStringType.Tex,  new RichStringSign("<texture","/>")},
            };

            internal static RichStringType CheckType(string text)
            {
                if (string.IsNullOrEmpty(text))
                {
                    return RichStringType.SimpleLab;
                }
                else
                {
                    foreach (var item in match_type_dict)
                    {
                        if (text.StartsWith(item.Value.StartSign) && text.Contains(item.Value.EndSign))
                            return item.Key;
                    }
                }
                return RichStringType.SimpleLab;
            }


            /// <summary>
            /// 截取字符串
            /// </summary>
            /// <returns>截取到的字符串</returns>
            internal static string SpliteRichStringByType(RichStringType rs_type, string txt)
            {
                string str = "";
                if (rs_type == RichStringType.Lab || rs_type == RichStringType.Url)
                {
                    var index = txt.IndexOf(">");
                    str = txt.Substring(0, index + 1);
                    str = str + match_type_dict[rs_type].EndSign;
                }
                else if (rs_type == RichStringType.SimpleLab)
                {
                    var index = txt.IndexOf('<');
                    if (index == -1) str = txt;
                    else if (index == 0)
                    {
                        var next_index = txt.IndexOf('<', 1);
                        str = next_index >= 0 ? txt.Substring(0, next_index) : txt;
                    }
                    else str = txt.Substring(0, index);
                }
                else
                {
                    var end_sign = match_type_dict[rs_type].EndSign;
                    var index = txt.IndexOf(end_sign);
                    if (index < 0) str = null;
                    else str = txt.Substring(0, index + end_sign.Length);
                }

                return str;
            }

            /// <summary>
            /// 接取整个元素数据
            /// </summary>
            /// <param name="txt"></param>
            internal static string SpliteWholeRichStringByType(RichStringType rs_type, string txt)
            {
                string str = "";
                if (rs_type == RichStringType.SimpleLab)
                {
                    var index = txt.IndexOf('<');
                    if (index == -1) str = txt;
                    else str = txt.Substring(0, index - 1);
                }
                else
                {
                    var sign = match_type_dict[rs_type];
                    var count = 1;
                    var start_index = txt.IndexOf(sign.StartSign) ;
                    var begin_index = start_index;
                    var can_continue = true;
                    if (start_index > -1)
                    {
                        start_index += sign.StartSign.Length;
                        var _start = 0;
                        var _end = 0;
                        do
                        {
                            _start = txt.IndexOf(sign.StartSign, start_index);
                            _end = txt.IndexOf(sign.EndSign, start_index);
                            if(_start >= 0 && _end >= 0)
                            {
                                if(_end < _start)
                                {
                                    --count;
                                    start_index = _end + sign.EndSign.Length;
                                }
                                else
                                {
                                    ++count;
                                    start_index = _start + sign.StartSign.Length;
                                }
                            }
                            else if(_end >= 0)
                            {
                                --count;
                                start_index = _end + sign.EndSign.Length;
                            }
                            else
                            {
                                str = null;
                                can_continue = false;
                            }

                            if(count == 0 && can_continue)
                            {
                                return txt.Substring(begin_index, _end + sign.EndSign.Length);
                            }
                            
                        } while (count != 0 && can_continue);
                    }
                }
                return str;
            }

            /// <summary>
            /// 从字符串里面移除第一个指定类型的数据
            /// </summary>
            /// <returns>返回移除后的字符串</returns>
            internal static string RemoveRichStringByType(RichStringType rs_type, string txt, string remove_str)
            {
                string str = "";
                if (rs_type == RichStringType.Lab || rs_type == RichStringType.Url)
                {
                    remove_str = remove_str.Substring(0, remove_str.IndexOf(">") + 1);
                    var length = remove_str.Length;
                    var temp_txt = txt.Substring(0, length);
                    if (temp_txt == remove_str)
                    {
                        var _str = txt.Remove(0, length);
                        var count = 1;
                        var start_index = 0;
                        var sign = match_type_dict[rs_type];
                        var can_continue = true;
                        do
                        {
                            var _start = _str.IndexOf(sign.StartSign, start_index);
                            var _end = _str.IndexOf(sign.EndSign, start_index);
                            if (_end >= 0 && _start >= 0)
                            {
                                if (_end < _start)
                                {
                                    --count;
                                    start_index = _end;
                                }
                                else
                                {
                                    ++count;
                                    start_index = _end + sign.StartSign.Length;
                                }
                            }
                            else if (_end >= 0)
                            {
                                --count;
                                start_index = _end;
                            }
                            else
                            {
                                //找不到结束标志
                                can_continue = false;
                            }

                            if (can_continue && count == 0)
                            {
                                _str = _str.Remove(_end, sign.EndSign.Length);
                                str = _str;
                                can_continue = false;
                            }
                        } while (can_continue);
                    }
                }
                else
                {
                    var length = remove_str.Length;
                    var temp_txt = txt.Substring(0, length);
                    if (temp_txt == remove_str)
                    {
                        str = txt.Remove(0, length);
                    }
                }
                return str;
            }

            internal static Dictionary<string, string> GetProperty(string txt)
            {
                // 先将值变成特定的字符串，然后再替换回来，这样做能够帮助值不会被改动
                var cur_save_index = 0;
                var value_dict = new Dictionary<string, string>();
                var start_index = 0;
                bool can_continue = true;
                while (txt.Contains("'") && can_continue)
                {
                    var index = txt.IndexOf("'");
                    if (index > -1)
                    {
                        start_index = index;
                        // 没有找到结束符
                        var next_index = txt.IndexOf("'", start_index + 1);
                        if (next_index == -1)
                        {
                            can_continue = false;
                        }
                        else
                        {
                            string key = "p" + cur_save_index++;
                            var sub_count = next_index + 1 - start_index;
                            string val = txt.Substring(start_index, sub_count);
                            value_dict.Add(key, val);

                           txt = txt.Remove(start_index, sub_count);
                           txt = txt.Insert(start_index, key);
                        }
                    }
                }

                var dict = new Dictionary<string, string>();
                var strs = txt.Split('|');
                foreach (var str in strs)
                {
                    var _str = str.Split('=');
                    if (_str.Length == 2)
                    {
                        var key = _str[1].TrimStart(' ').TrimEnd(' ');
                        var val = value_dict[key].TrimStart(' ').TrimEnd(' ');
                        dict.Add(_str[0].TrimStart(' ').TrimEnd(' '), val);
                    }

                }
                return dict;
            }

            internal static string MakePropertyToRichString(Dictionary<string,string> property,RichStringType rs_type)
            {
                StringBuilder str = new StringBuilder();
                if (match_type_dict.ContainsKey(rs_type))
                {
                    var sign = match_type_dict[rs_type];
                    str.Append(sign.StartSign);
                    str.Append(" ");
                }

                var count = property.Count;
                var index = 1;
                foreach (var item in property)
                {
                    var key = item.Key;
                    var val = item.Value;
                    str.Append(key);
                    str.Append("=");
                    str.Append(val);
                    if (index++ < count) str.Append("|");
                }

                if (match_type_dict.ContainsKey(rs_type))
                {
                    var sign = match_type_dict[rs_type];
                    str.Append(" />");
                }
                return str.ToString();
            }

            internal static string GetPureTxt(string txt, RichStringType rs_type)
            {
                if (rs_type == RichStringType.SimpleLab) return txt;
                var sign = match_type_dict[rs_type];
                txt = txt.Replace(sign.EndSign, "");
                txt = txt.Replace(sign.StartSign, "");
                if (rs_type == RichStringType.Lab || rs_type == RichStringType.Url)
                {
                    txt = txt.Replace("/>", "");
                }
                return txt;
            }

            internal static bool CheckIsEndSign(string val, int i)
            {
                bool found = false;
                foreach (var item in match_type_dict)
                {
                    var temp_txt = val.Substring(0, i + 1);
                    var end_sign = item.Value.EndSign;
                    if (temp_txt.EndsWith(end_sign))
                    {
                        found = true;
                        break;
                    }
                }
                return found;
            }

            internal static string ChangeLabAndUrlEndSign(string txt,RichStringType rs_type)
            {
                if(rs_type == RichStringType.Lab || rs_type == RichStringType.Url)
                {
                    var sign = match_type_dict[rs_type];
                    txt = txt.TrimEnd(' ');
                    if(txt.EndsWith(sign.EndSign))
                    {
                        txt = txt.Replace(">" + sign.EndSign, "/>");
                    }
                }
                return txt;
            }

            internal static string MakeLabelElement(string txt)
            {
                if (string.IsNullOrEmpty(txt)) return "";
                var lab_sign = RichStringTools.match_type_dict[RichStringType.Lab];
                var val = lab_sign.StartSign + " text = '" + RichTxtTool.ReplaceSpecialSymbol(txt) + "'" + lab_sign.EndSign;
                return val;
            }
        }
    }
}